<?php

namespace App\Backend\Http\Controllers;

use App\Admin;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $data['admins'] = Admin::paginate(20);;
        return view('backend::admin.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($id)
    {
        $data['admin'] = Admin::find($id);
        return view('backend::admin.create',$data);
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        if($request->id == 0){
            $data = $request->validate([
                  'id' => 'required',
                  'name' => 'required|unique:admins',
                  'password' => 'sometimes|required',
                  'level'  => 'required',
            ]);
            $data['password'] = bcrypt($request->password);
            Admin::create($data);
        }else{
            $data = $request->validate([
                'id' => 'required',
                'name' => 'required',
                'password' => 'sometimes|required',
                'level'  => 'required',
            ]);
            $data['password'] = bcrypt($request->password);
            Admin::where('id',$request->id)->update($data);
        }
        return redirect('/backend/admin');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy()
    {
    }
}
